<template>
  <div style="line-height: 0;">
    <div style="margin-top: 15px; margin-bottom: 15px; line-height: 0; text-align: left;">
      <div style="margin-top: 15px; margin-bottom: 15px; line-height: 0; text-align: left;">
        <el-row>
          <label>树苗名称：</label>
          <el-input style="width: 200px;" prefix-icon="el-icon-search" v-model="params.treename" placeholder="请输入树苗名称"></el-input>
          &nbsp;&nbsp;&nbsp;
          <el-button type="warning" round style="margin-left: 10px;" @click="findTree">查询</el-button>
          &nbsp;&nbsp;&nbsp;
          <el-button type="warning" round style="margin-left: 10px;" @click="reset">重置</el-button>
          &nbsp;&nbsp;&nbsp;
          <el-button type="warning" round style="margin-left: 10px;">上架树苗</el-button>
        </el-row>
      </div>
      <div>
        <el-table
            :data="tableData"
            border
            style="width: 100%">
          <el-table-column prop="tree_id" v-if="false"></el-table-column>
          <el-table-column prop="treename" label="树苗名称" width="180"></el-table-column>
          <el-table-column prop="treetime" label="树苗年份" width="180"></el-table-column>
          <el-table-column prop="treeprice" label="树苗价格" width="180"></el-table-column>
          <el-table-column prop="treetypename" label="树苗种类" width="180"></el-table-column>
          <el-table-column prop="sellnum" label="在售数量"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
              <el-popconfirm title="确定要下架吗?" @confirm="del(scope.row)">
                <el-button slot="reference" type="danger" style="margin-left: 5px;">树苗下架</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <div class="block" style="text-align: center;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--编辑-->
    <div >
      <el-dialog title="树苗信息" :visible.sync="dialogFormVisible" >
        <el-form :model="form" style="text-align: center;">
          <el-form-item label="树苗名称" :label-width="formLabelWidth" disabled>
            <el-input v-model="form.treename" autocomplete="off" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="树苗年份" :label-width="formLabelWidth">
            <el-input v-model="form.treetime" autocomplete="off" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="树苗价格" :label-width="formLabelWidth">
            <el-input v-model="form.treeprice" autocomplete="off" style="width: 80%;"></el-input>
          </el-form-item>
          <el-form-item label="树苗种类" :label-width="formLabelWidth">
            <el-select v-model="form.treetypename" style="width: 80%;" >
              <el-option v-for="item in treeform"  :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" >
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>

</template>
<script>
import request from "@/utils/request";
import {findTree} from "@/api/tree";
export default {
  data() {
    return {
      params: {
        username:JSON.parse(sessionStorage.getItem("user")).username,
        treename: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      formLabelWidth: '190px',
      dialogFormVisible: false,
      form : {},
      activeName:"first",
      treeform:{},
    }
  },
  //在页面加载的时候，做一些事情
  created() {
    this.findTree();
    this.loadtreetypename();
  },
  //定义一些页面上触发事件调用的方法
  methods:{
    loadtreetypename(){
      request.get("treename/loadtreetypename").then(res => {
        if (res.code === '0') {
          this.treeform = res.data
          console.log(this.treeform)
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.findTree();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum;
      this.findTree();
    },
    findTree(){
      findTree(this.params).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else{
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    edit(obj){
      this.form = obj;
      this.dialogFormVisible = true;
    },
    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 5,
        treename: '',
      }
      this.findTree();
    },
    del(tree) {
      request.delete(`/tree/del/${tree.treeId}`).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.findTree();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    submit(){
      request.post("/tree/treeedit",this.form).then(res => {
        if(res.code === '0'){
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          location.reload();
        }
        else{
          this.$message({
            message: res.msg,
            type: 'error'
          })
        }
      })
      this.dialogFormVisible = false;
      this.findTree();
    }
  }
}
</script>